/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package item;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import myJPA.Model;
import transactions.Reservation;

/**
 *
 * @author Nacer Khalil
 */
public class TitleReportMgt {

    public static String produceBasicInfoReport()
    {
        try
        {
            EntityManager manager = Model.getEntityManager();
            List<Item> list = (List<Item>) manager.createNamedQuery("getAllItems").getResultList();
            String report = "";
            for(Item item: list)
            {
                report += "Title: "+ item.getTitle() +", Author: "+item.getAuthor()+", Producer: "+item.getProducer()
                       +", Genre: "+item.getGenre()+", Production year: "+item.getProductionYear()
                       +", copies: " +item.getNumberCopies()+", available: "+item.getAvailableCopies()+"\n";
            }
            
            return report;
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            return null;
        }
    }
    
    public static String produceDetailedReport()
    {
        try
        {
            EntityManager manager = Model.getEntityManager();
            List<Item> list = (List<Item>) manager.createNamedQuery("getAllItems").getResultList();
            
            String report = "";
            for(Item item: list)
            {
                
                report += "Title: "+ item.getTitle() 
                       + ", copies: " +item.getNumberCopies()+", available: "+item.getAvailableCopies()+", rented: ";
                if(item.getRentals() != null)
                    report += item.getRentals().size()+", on hold: ";
                else
                    report += "0, on hold: ";
                System.out.println("id: "+item.getId());
                Query query = manager.createNamedQuery("findOnHoldReservations");
                query.setParameter("item", item);
                List<Reservation> reservations = query.getResultList();
                if(reservations != null)
                    report += reservations.size()+"\n";
                else
                    report += "0\n";
            }
            
            return report;
        }
        catch(Exception ex)
        {
            System.out.println("There is an exception");
            ex.printStackTrace();
            return null;
        }
    }
    
    public static String produceReservationItemReport()
    {
        try
        {
            EntityManager manager = Model.getEntityManager();
            List<Item> list = (List<Item>) manager.createNamedQuery("getAllItems").getResultList();
            String report = "";
            for(Item item: list)
            {
                report += "Title: "+ item.getTitle() +", number of reservations: ";
                Query query = manager.createNamedQuery("findNotOnHoldReservations");
                query.setParameter("item", item);
                List<Reservation> reservations = query.getResultList();
                if(reservations != null)
                    report+= reservations.size()+"\n"; 
                else
                    report += "0\n";
            }
            
            return report;
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            return null;
        }
    }
}
